//
//  BaseViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/6/28.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController,NetProtocol {
    func method(for name: String) -> NetMethod {
        return .post
    }
    
    func reset()->Void{
        
    }
    
    var names: Array<String>{
        get{
            return []
        }
    }
    
    func path(for name: String) -> String {
        return ""
    }
    
    func param(for name: String) -> Dictionary<String,Any> {
        return ["":""]
    }
    
    func success(for name: String) -> NetSuccess {
        func success(data: JSON)->Void{
            
        }
        return success;
    }
    
    func failBlock(for name: String) -> NetFail {
        func fail(code: Error)->Void{
            self.showError(code.localizedDescription)
        }
        return fail
    }
    
    func strategy(for name: String) -> (Bool,String?,(()->Void)?) {
        return (true,nil,nil)
    }
    
    func files(for name: String) -> Array<Data> {
        return [Data()]
    }
    
    func filename(for name: String)->String {
        return "file"
    }
    
    func load(with name: String) -> Void {
        let result = strategy(for: name)
        
        if result.0 == false {
            if result.2 != nil {
                result.2!();
            }else{
                self.alert(result.1)
            }
            return
        }
        self.beginLoading()
        switch self.method(for: name) {
        case .get:
            NetManager.get(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
            break
        case .upload:
            let files = self.files(for: name)
            for file in files {
                NetManager.upload(self.path(for: name), self.param(for: name), [file], name: filename(for: name), suc: { (source) in
                    self.endLoading()
                    self.success(for: name)(source)
                }, fail: { (error) in
                    self.endLoading()
                    self.failBlock(for: name)(error)
                })
            }
        default:
            NetManager.post(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
        }
    }
    
    
    var toolbarview = UIView()
    
    @objc func canceledit()->Void{
        self.view.endEditing(true)
    }
    
    @objc func endedit()->Void{
        self.view.endEditing(true)
    }
    
    @objc func popobjc()->Void{
        pop()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        if self.navigationController != nil {
            if let index = self.navigationController?.children.firstIndex(of: self) {
                if index > 0 {
                    let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_whiteback")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
                    self.navigationItem.leftBarButtonItem = back
                } else {
                    self.navigationItem.leftBarButtonItem = nil
                }
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        toolbarview.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: UIScreen.main.bounds.size.width, height: 40))
        toolbarview.layer.borderWidth = 0.5
        toolbarview.layer.borderColor = UIColor.hex("999999").cgColor
        let cancelButton = UIButton.init(type: .system)
        cancelButton.setTitleColor(UIColor.black, for: .normal)
        cancelButton.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 100, height: 40))
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.addTarget(self, action: #selector(canceledit), for: .touchUpInside)
        toolbarview.addSubview(cancelButton)
        let sureButton = UIButton.init(type: .system)
        sureButton.setTitleColor(UIColor.black, for: .normal)
        sureButton.frame = CGRect.init(origin: CGPoint.init(x: UIScreen.main.bounds.size.width - 100, y: 0), size: CGSize.init(width: 100, height: 40))
        sureButton.setTitle("确定", for: .normal)
        sureButton.addTarget(self, action: #selector(endedit), for: .touchUpInside)
        toolbarview.addSubview(sureButton)
        toolbarview.backgroundColor = UIColor.white
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
    override var shouldAutorotate: Bool{
        get{
            return true
        }
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get{
            return UIInterfaceOrientationMask.portrait
        }
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        get{
            return UIInterfaceOrientation.portrait
        }
    }
    
}
